package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.ActivityDao;
import com.keepsport.keepsportapi.dto.ActivityAddDto;
import com.keepsport.keepsportapi.entity.Activity;
import com.keepsport.keepsportapi.service.intf.ActivityService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityDao dao;
    @Override
    public R save(ActivityAddDto dto,int token) {
        if(dto!=null){
            Activity activity = new Activity();
            BeanUtils.copyProperties(dto,activity);
            activity.setUid(token);
            if(dao.insert(activity)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R audit(int id, int flag) {
        //校验
        if(id>0&&(flag>0&&flag<3)){
            //更改状态
            if(dao.updateFlag(id,flag)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
    //1.未开始 2.进行中
    @Override
    public R query(int type) {
        //校验
        if(type>0&&type<3){
            //条件构造器
         QueryWrapper<Activity> wrapper = new QueryWrapper<>();
            Date date = new Date();
            switch (type){
                case 1://未开始
                {    wrapper.eq("flag",2);
                    wrapper.gt("sdate",date);
                    wrapper.orderByAsc("sdate");
                }break;
                case 2://进行中
                {    wrapper.eq("flag",2);
                    wrapper.gt("edate",date);
                    wrapper.lt("sdate",date);
                    wrapper.orderByDesc("edate");
                }break;


            }
            return R.ok(dao.selectList(wrapper));

        }

        return R.fail();
    }
}
